//
//  ABStatusCell.swift
//  microblog
//
//  Created by 安波 on 15/10/31.
//  Copyright © 2015年 anbo. All rights reserved.
//

import UIKit
import FFAutoLayout

// 定义全局变量  间隔
let statusCellMargin: CGFloat = 8

class ABStatusCell: UITableViewCell {

    // MARK: - 属性
    /// 配图宽度约束
    var pictureViewWidthCon: NSLayoutConstraint?
    
    /// 配图高度约束
    var pictureViewHeightCon: NSLayoutConstraint?
    
    /// 微博模型
    var status: ABStatus? {
        didSet{
            // 将模型赋值给 topView
            topView.status = status
            
            // 设置微博内容
            contentLabel.text = status?.text
            //  注意，一定要给pictureView赋值，才能显示出效果
            pictureView.status = status
            
            // 测试微博配图的高度
//            pictureViewHeightCon?.constant = 290
//            let row = arc4random_uniform(1000) % 4
//            pictureViewHeightCon?.constant = CGFloat(row) * 90
            let size = pictureView.calcViewSize()
//            print("size: \(size)")
            
            // 根据计算尺寸，重新配置宽高约束
            pictureViewWidthCon?.constant = size.width
            pictureViewHeightCon?.constant = size.height
            
        }
    }
    
    // MARK: - 构造函数
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        prepareUI()
    }
    
    // MARK: - 准备UI
    func prepareUI(){
        // 添加子控件
        contentView.addSubview(topView)
        contentView.addSubview(contentLabel)
        contentView.addSubview(pictureView)
        contentView.addSubview(bottomView)
        
        // 添加约束
        // 顶部视图
        topView.ff_AlignInner(ff_AlignType.TopLeft, referView: contentView, size: CGSize(width: UIScreen.width(), height: 44))
        
        // 微博内容
        contentLabel.ff_AlignVertical(ff_AlignType.BottomLeft, referView: topView, size: nil, offset: CGPoint(x: statusCellMargin, y: statusCellMargin * 2))
        // 设置宽度
        contentView.addConstraint(NSLayoutConstraint(item: contentLabel, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: UIScreen.width() - 2 * statusCellMargin))
        
        // 微博配图
//        let con = pictureView.ff_AlignVertical(ff_AlignType.BottomLeft, referView: contentLabel, size: CGSize(width: 290, height: 290), offset: CGPoint(x: 0, y: statusCellMargin))
//        pictureViewWidthCon = pictureView.ff_Constraint(con, attribute: NSLayoutAttribute.Width)
//        pictureViewHeightCon = pictureView.ff_Constraint(con, attribute: NSLayoutAttribute.Height)
        
        // 底部视图
//        bottomView.ff_AlignInner(ff_AlignType.BottomLeft, referView: pic, size: CGSize(width: UIScreen.width(), height: 44))
        bottomView.ff_AlignVertical(ff_AlignType.BottomLeft, referView: pictureView, size: CGSize(width: UIScreen.width(), height: 44), offset: CGPoint(x: -statusCellMargin, y: statusCellMargin))
        
        // 添加contentLabel底部和pictureView的顶部重合
//        contentView.addConstraint(NSLayoutConstraint(item: contentLabel, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: pictureView, attribute: NSLayoutAttribute.Top, multiplier: 1, constant: 0))
        // 添加contentView底部和bottomView的底部重合
        /*
        
        注意：当配图的  高度约束  修改后，添加contentView底部和bottomView的底部重合，导致系统计算cell高度约束出错，不能让系统根据子控件的约束来计算contentView高度的约束。应注释该约束
        
        */
//        contentView.addConstraint(NSLayoutConstraint(item: contentView, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: bottomView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0))
        
    }
    
    // 设置cell的模型，cell会根据模型，重新设置内容、更新约束，获取子控件的最大Y值
    // 返回cell的高度
    func rowHeight(status: ABStatus) -> CGFloat {
        // 设置cell的模型
        self.status = status
        
        // 更新约束
        layoutIfNeeded()
        
        // 获取子控件的最大Y值
        let maxY = CGRectGetMaxY(bottomView.frame)
        
        // 返回cell的高度
        return maxY
        
    }
    
    // MARK: - 懒加载
    /// 顶部视图
    private lazy var topView: ABStatusTopView = ABStatusTopView()
    
    /// 微博内容
    lazy var contentLabel: UILabel = {
        //创建label
        let label = UILabel(fonsize: 16, textColor: UIColor.blackColor())
        
        // 自动换行
        label.numberOfLines = 0
        
        // 返回
        return label
    }()
    
    /// 底部视图
    lazy var bottomView: ABStatusBottomView = ABStatusBottomView()
    
    /// 添加微博配图
    lazy var pictureView: ABStatusPictureView = ABStatusPictureView()
    

}
